home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / gfx / 3d / Skulpt_src.lha / sKulpt-src / Ami-Texture.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-04  |  9.4 KB  |  317 lines

  1. #define STRICT
  2.  
  3. // Includes standard Windows
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <memory.h>
  10. #include <stdio.h>
  11.  
  12. // Includes D3D
  13. #define  D3D_OVERLOADS
  14. #include <ddraw.h>
  15. #include <d3d.h>
  16.  
  17. // Includes utilitaires D3D
  18. #include "d3dmath.h"
  19. #include "d3dutil.h"
  20. #include "D3DEnum.h"
  21.  
  22. #include <d3dx.h>
  23.  
  24. // Constantes
  25. #include "const.h"
  26.  
  27. // Types
  28. #include "types.h"
  29.  
  30. // Variables globales projet
  31. #include "vars.h"
  32.  
  33. // Prototypes fonctions autres modules
  34. #include "proto.h"
  35.  
  36. // Macros
  37. #include "macros.h"
  38.  
  39. // Charger une image ARGB en mémoire à partir d'un fichier avec les dadatypes
  40. UBYTE *LoadTextureFromFile(APTR hPtr, char *sFileName, UBYTE Opacity, int *iWidth, int *iHeight)
  41. {
  42.     BitMapHeader *bh;
  43.     UBYTE *hImg = NULL;
  44.     HGDIOBJ hObject = NULL;
  45.     pdtBlitPixelArray pBPA;
  46.     DOUBLE fDim, fDum;
  47.     int iCnt;
  48.  
  49.     // Charger la texture avec picture.datatype et la sous classe associée au type de fichier image
  50.     if (!(hObject = NewDTObject(sFileName,
  51.         DTA_SourceType,        DTST_FILE,
  52.         DTA_GroupID,        GID_PICTURE,
  53.         PDTA_DestMode,      PMODE_V43,
  54.         TAG_DONE)))
  55.     {
  56.         vTrace("*** E0082 : format du fichier texture [%s] inconnu", sFileName);
  57.         goto __Exit;
  58.     }
  59.  
  60.     // Récupérer le bitmap header pour avoir les dimensions
  61.     GetDTAttrs(hObject, PDTA_BitMapHeader, &bh, TAG_DONE);
  62.     if (!bh)
  63.     {
  64.         vTrace("*** E0083 : structure texture [%s] incompatible", sFileName);
  65.         goto __Exit;
  66.     }
  67.  
  68.     *iWidth = bh -> bmh_Width; *iHeight = bh -> bmh_Height;
  69.  
  70. #if 1 // N'autoriser que des textures carrées coté puissance de 2
  71.     // Vérifier que la texture est carrée
  72.     if (*iWidth != *iHeight)
  73.     {
  74.         vTrace("*** E0110 : texture %s non carrée (%d x %d)", sFileName, *iWidth, *iHeight);
  75.         goto __Exit;
  76.     }
  77.  
  78.     // Vérifier que ses dimensions sont un multiple de 2
  79.     fDim = log(*iWidth) / log(2);
  80.     if (modf(fDim, &fDum) > 1.e-5)
  81.     {
  82.         vTrace("*** E0111 : dimensions texture %s non puissance de 2 (%d x %d)", sFileName, *iWidth, *iHeight);
  83.         goto __Exit;
  84.     }
  85. #endif
  86.  
  87.     // Allouer le buffer pour y copier le bitmap
  88.     if (!(hImg = (UBYTE *) AllocVec(4 * *iWidth * *iHeight, MEMF_PUBLIC)))
  89.     {
  90.         vTrace("*** E0084 : allocation buffer texture %s (%d x %d)", sFileName, *iWidth, *iHeight);
  91.         goto __Exit;
  92.     }
  93.  
  94.     // Appeller la méthode picture.datatype::PDTM_READPIXELARRAY pour copier le bitmap de l'objet datatype dans notre buffer
  95.     pBPA.MethodID = PDTM_READPIXELARRAY;
  96.     pBPA.pbpa_PixelData = (APTR) hImg;
  97.     pBPA.pbpa_PixelFormat = PBPAFMT_ARGB;
  98.     pBPA.pbpa_PixelArrayMod = *iWidth * 4;
  99.     pBPA.pbpa_Left = 0;
  100.     pBPA.pbpa_Top = 0;
  101.     pBPA.pbpa_Width = *iWidth;
  102.     pBPA.pbpa_Height = *iHeight;                  
  103.  
  104. #ifndef __PPC__
  105.     DoMethodA(hObject, Msg(&pBPA));
  106. #else
  107.     // K68_DoMethodA(hObject, Msg(&pBPA));
  108. #endif
  109.  
  110.     // Régler l'alpha
  111.     for (iCnt = 0 ; iCnt < bh -> bmh_Width * bh -> bmh_Height ; iCnt++)
  112.         hImg[iCnt * 4] = Opacity;
  113.  
  114. __Exit:
  115.     // Libérer l'objet datatype (puisqu'on a copié le bitmap dans un buffer)
  116.     if (hObject) DisposeDTObject(hObject);
  117.  
  118.     return hImg;
  119. }
  120.  
  121. void vXRefMaterials2Textures(void)
  122. {
  123.     vTrace("### Référencement croisé textures / matériaux ...");
  124.  
  125.     // Référencer tous les matériaux texturés
  126.     for (int iMtrl = 0 ; iMtrl <= iMtrlLastUsed ; iMtrl++)
  127.     {
  128.         if  (!Materials[iMtrl].bEnabled) continue;
  129.  
  130.         Materials[iMtrl].iTexture = 0;
  131.  
  132.         if (Materials[iMtrl].bTextured)
  133.         {
  134.             Materials[iMtrl].bTextured = FALSE;
  135.             for (int iText = 0 ; iText < XDC_NUMTEX ; iText++)
  136.                 if (Textures[iText].hTexMap && Textures[iText].hTexture && !stricmp(Materials[iMtrl].sTexName, Textures[iText].sName))
  137.                 {
  138.                      vTrace("\tMatch matériau n° %d (%s) avec texture n° %d (%s)", iMtrl, Materials[iMtrl].sName, iText, Textures[iText].sName);
  139.                      Materials[iMtrl].iTexture = iText;
  140.                      Materials[iMtrl].bTextured = TRUE;
  141.                      iText = XDC_NUMTEX;
  142.                 }
  143.  
  144.             if (!Materials[iMtrl].bTextured)
  145.                 vTrace("*** E0112 : texture [%s] introuvable", Materials[iMtrl].sTexName);
  146.         }
  147.     }
  148.     vTrace("### Référencement terminé");
  149. }
  150.  
  151. BOOL GenTextures(W3D_Context* hW3DC)
  152. {
  153.     int iText;
  154.     ULONG lError;
  155.  
  156.     vTrace("### Installation des textures dans le contexte 3D ...");
  157.  
  158.     // Uploader toutes les textures disponibles dans le contexte W3D
  159.     for (iText = 0 ; iText <= iTextLastUsed ; iText++)
  160.         if (Textures[iText].hTexMap)
  161.         {
  162.             Textures[iText].hTexture = W3D_AllocTexObjTags(hW3DC, &lError,
  163.                 W3D_ATO_IMAGE,      (ULONG) Textures[iText].hTexMap,
  164.                 W3D_ATO_FORMAT,     W3D_A8R8G8B8,
  165.                 W3D_ATO_WIDTH,      Textures[iText].iWidth,
  166.                 W3D_ATO_HEIGHT,     Textures[iText].iHeight,
  167.                 TAG_DONE);
  168.  
  169.             if (!Textures[iText].hTexture || lError != W3D_SUCCESS)
  170.             {
  171.                 switch(lError)
  172.                 {
  173.                     case W3D_ILLEGALINPUT:          vTrace("*** E0085 : format incompatible");              break;
  174.                     case W3D_NOMEMORY:              vTrace("*** E0086 : plus de mémoire");                  break;
  175.                     case W3D_UNSUPPORTEDTEXSIZE:    vTrace("*** E0087 : taille de texture incompatible");   break;
  176.                     case W3D_NOPALETTE:             vTrace("*** E0088 : texture chunky sans palette");      break;
  177.                     case W3D_UNSUPPORTEDTEXFMT:        vTrace("*** E0089 : format inconnu");                     break;
  178.                     default:                        vTrace("*** E0090 : erreur interne");
  179.                 }
  180.                 FreeVec(Textures[iText].hTexMap);
  181.                 memcpy(&Textures[iText], &Textures[iText + 1], (iTextLastUsed-- - iText) * sizeof(gTex));
  182.                 continue;
  183.             }
  184.  
  185.             vTrace("\tTexture W3D [%s] installée, taille %ld × %ld", Textures[iText].sName, Textures[iText].iWidth, Textures[iText].iHeight);
  186.  
  187.             // Set texture wrapping mode to on.
  188.             W3D_SetWrapMode(hW3DC, Textures[iText].hTexture, W3D_REPEAT, W3D_REPEAT, NULL);
  189.  
  190.             // Set blending (W3D_REPLACE, W3D_DECAL, W3D_MODULATE, W3D_BLEND) (devrait se faire par material)
  191.             W3D_SetTexEnv(hW3DC, Textures[iText].hTexture, W3D_REPLACE, NULL);
  192.  
  193.             // Set bilinear filter
  194.             W3D_SetFilter(hW3DC, Textures[iText].hTexture, W3D_LINEAR, W3D_LINEAR);
  195.         }
  196.  
  197.     vTrace("### Installation des textures terminée");
  198.  
  199.     // Référencer les matériaux
  200.     vXRefMaterials2Textures();
  201.  
  202.     return TRUE;
  203. }
  204.  
  205. void vCloseTextures(W3D_Context *hW3DC)
  206. {
  207.     vTrace("### Suppression des textures du contexte 3D ...");
  208.  
  209.     W3D_FlushTextures(hW3DC);
  210.  
  211.     for (int iText = 0 ; iText < XDC_NUMTEX ; iText++)
  212.         if (Textures[iText].hTexMap)
  213.             if (Textures[iText].hTexture)
  214.             {
  215.                 W3D_FreeTexObj(hW3DC, Textures[iText].hTexture);
  216.                 Textures[iText].hTexture = NULL;
  217.             }
  218.  
  219.     vTrace("### Suppression des textures du contexte 3D terminée");
  220. }
  221.  
  222. BOOL bLoadTextures(void)
  223. {
  224.     int iText;
  225.     BOOL bOk                = FALSE;
  226.     BPTR hTexDir            = Lock("Textures", ACCESS_READ);
  227.     ExAllData *hDirData     = (ExAllData *) AllocVec(4096, MEMF_PUBLIC);
  228.     ExAllControl *hExamine  = (ExAllControl *) AllocDosObject(DOS_EXALLCONTROL,NULL);
  229.  
  230.     vTrace("### Chargement des textures du catalogue...");
  231.  
  232.     // RAZ textures
  233.     for (iText = 0 ; iText < XDC_NUMTEX ; iText++)
  234.         Textures[iText].hTexMap = NULL;
  235.  
  236.     if (!hTexDir)
  237.     {
  238.         vTrace("*** E0091 : lecture dir textures");
  239.         goto __bltFail;
  240.     }
  241.  
  242.     if (!hDirData)
  243.     {
  244.         vTrace("*** E0092 : allocation buffer");
  245.         goto __bltFail;
  246.     }
  247.  
  248.     if (!hExamine)
  249.     {
  250.         vTrace("*** E0093 : allocation contrôle examine");
  251.         goto __bltFail;
  252.     }
  253.  
  254.     hExamine -> eac_LastKey = 0;
  255.     iText = 0;
  256.     do
  257.     {
  258.         ExAllData *hFileData;
  259.         bOk = ExAll(hTexDir, hDirData, 4096, ED_NAME, hExamine);
  260.         if ((!bOk) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  261.         {
  262.             // ExAll failed abnormally
  263.             break;
  264.         }
  265.         if (hExamine -> eac_Entries == 0)
  266.         {
  267.             // ExAll failed normally with no entries
  268.             continue;                   // ("bOk" is *usually* zero)
  269.         }
  270.         hFileData = (ExAllData *) hDirData;
  271.         do
  272.         {
  273.             // use hFileData here
  274.             strcpy(Textures[iText].sName, "Textures/");
  275.             strcat(Textures[iText].sName, hFileData -> ed_Name);
  276.  
  277.             if (Textures[iText].hTexMap = LoadTextureFromFile( NULL,
  278.                                 Textures[iText].sName,
  279.                                 0x7F,
  280.                                 &Textures[iText].iWidth,
  281.                                 &Textures[iText].iHeight))
  282.  
  283.             {
  284.                 vTrace("\tTexture n° %d : [%s] (%d x %d) chargée", iText, hFileData -> ed_Name, Textures[iText].iWidth, Textures[iText].iHeight);
  285.                 strcpy(Textures[iText].sName, hFileData -> ed_Name);
  286.                 iText++;
  287.             }
  288.  
  289.             // get next hFileData
  290.             hFileData = hFileData -> ed_Next;
  291.         }
  292.         while (hFileData);
  293.  
  294.     } while (bOk);
  295.   
  296.     bOk = TRUE;
  297.     iTextLastUsed = iText - 1;
  298.  
  299. __bltFail:
  300.     if (hExamine)   FreeDosObject(DOS_EXALLCONTROL, hExamine);
  301.     if (hDirData)   FreeVec(hDirData);
  302.     if (hTexDir)    UnLock(hTexDir);
  303.  
  304.     vTrace("### Chargement des textures terminé");
  305.     return bOk;
  306. }
  307.  
  308. void vUnLoadTextures(void)
  309. {
  310.     // RAZ textures
  311.     for (int iText = 0 ; iText < XDC_NUMTEX ; iText++)
  312.         if (Textures[iText].hTexMap) FreeVec(Textures[iText].hTexMap);
  313. }
  314.  
  315.  
  316.  
  317.